package cn.bandeqing.base.aliyun;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;

@Slf4j
public class SmsUtil {

    public static final Client client = createClient();

    /**
     * 使用AK&SK初始化账号Client
     */
    @SneakyThrows
    public static Client createClient() {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tGjAgXnMjgzAb28rqsv")
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("YGERrwlAIbUV09A4Yrmk9JYERYFIui");
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    @SneakyThrows
    public static void sendSmsVerificationCode(String phoneNumbers, String code) {
        JSONObject entries = JSONUtil.ofObj();
        entries.set("code", code);
        sendSms(TemplateCode.SMS_296275117.name(), phoneNumbers, entries);
    }

//    public static void main(String[] args) {
//        sendSmsVerificationCode("17783738583", "123456");
//    }

    public static void sendSms(String templateCode, String phoneNumbers, JSONObject templateParam) {
        new Thread(() -> {
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName("斑得清");
            request.setTemplateCode(templateCode);
            request.setTemplateParam(templateParam.toString());
            log.info("request = {}", JSONUtil.toJsonStr(request));
            // {"phoneNumbers":"17783738583","signName":"斑得清","templateCode":"SMS_296275117","templateParam":"{\"code\":\"129837\"}"}
            SendSmsResponse response = null;
            try {
                response = client.sendSms(request);
            } catch (Exception e) {
                log.error("sendSms error", e);
            }
            // {"headers":{"access-control-allow-origin":"*","date":"Mon, 25 Mar 2024 06:55:27 GMT","content-length":"110","keep-alive":"timeout=25","x-acs-request-id":"76764341-086F-5565-BFD4-338FE8537922","connection":"keep-alive","content-type":"application/json;charset=utf-8","etag":"1OiERf2NMV59sTjmXJfb3kA0","access-control-expose-headers":"*","x-acs-trace-id":"92a487537d6aa7951216710c2e27f0ab"},"statusCode":200,"body":{"bizId":"532820111349727266^0","code":"OK","message":"OK","requestId":"76764341-086F-5565-BFD4-338FE8537922"}}
            log.info("response = {}", JSONUtil.toJsonStr(response));
        }).start();
    }

    /**
     * 发送客户待支付定金的通知
     *
     * @param orderId       132847928
     * @param customerName  xxx
     * @param customerPhone 17783738583
     * @param url           <a href="https://wxaurl.cn/oRfcVOU0Nnr">https://wxaurl.cn/oRfcVOU0Nnr</a> oRfcVOU0Nnr
     * @param shouldDeposit 100.0
     */
    public static void sendOrderWaitPayDeposit(Long orderId, String customerName, String customerPhone, String url, Double shouldDeposit) {
        JSONObject entries = JSONUtil.ofObj();
        entries.set("name", customerName);
        entries.set("orderId", orderId.toString());
        entries.set("money", shouldDeposit.toString());
        entries.set("ticket", url);
        sendSms(TemplateCode.SMS_465393847.name(), customerPhone, entries);
    }

//    public static void main(String[] args) {
//        System.out.println("https://wxaurl.cn/oRfcVOU0Nnr".substring(18));
//    }

}
